#include "shared_image_queue.hpp"
#include <iostream>
#include <opencv2/opencv.hpp>
//
//int main()
//{
//        cv::VideoCapture video("D:\\xm\\video\\1.avi");
//        if (!video.isOpened()) return 1;
//        cv::Mat frame, half_frame;
//        cv::namedWindow("show", 1);
//        CSharedImageQueueWriter writer;
//        writer.init("K1135_shared_memory", 640, 360);
//        while (true) {
//                video >> frame;
//                if (frame.empty()) break;
//                cv::resize(frame, half_frame, cv::Size(frame.cols / 2, frame.rows / 2));
//                writer.write(half_frame.data, half_frame.rows * half_frame.cols * half_frame.channels());
//                cv::imshow("show", half_frame);
//                if (cv::waitKey(30) == 27) break;
//        }
//        cv::destroyAllWindows();
//        return 0;
//}

int main()
{
        CSharedImageQueueReader reader;
        if (!reader.init("K1135_shared_memory")) {
                std::cerr << "reader init failed." << std::endl;
                return 1;
        }
        int image_width = 0, image_height = 0;
        reader.get_image_size(image_width, image_height);
        cv::Mat frame = cv::Mat::zeros(image_height, image_width, CV_8UC3);
        unsigned int index = reader.get_current_written_index();
        cv::namedWindow("reader", 1);
        while (true) {
                int result = reader.read(frame.data, frame.rows * frame.cols * frame.channels(), index);
                if (result == -1) break;
                if (result == 0) {
                        cv::waitKey(1);
                        continue;
                }
                cv::imshow("reader", frame);
                if (cv::waitKey(3) == 27) break;
                index += 1;
        }
        cv::destroyAllWindows();
        return 0;
}